<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar :radius="100" @input="inputChange" cancelButton="none" :focus="true"></uni-search-bar>
		</view>
		<!-- 搜索结果展示列表 -->
		<view class="sugg-list">
			<view class="sugg-item" v-for="(item,index) in searchResults" :key="index" @click="toDetail(item)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrow-right" size="16"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 延时器
				timer: null,
				// 搜索的关键词
				keyWord: '',
				// 搜索结果的列表
				searchResults: []
			}
		},
		methods: {
			// 输入框内容变化时触发
			inputChange(e) {
				// 搜索框防抖处理
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					// console.log(e)
					this.keyWord = e
					this.getSearchList()
				}, 300)
			},
			// 根据输入的关键词获取搜索数据
			async getSearchList() {
				// 搜索关键词为空时
				if (this.keyWord.length === 0) {
					this.searchResults = []
					return
				}
				let res = await uni.$http.get(`/api/public/v1/goods/qsearch?query=${this.keyWord}`)
				console.log(res)
				if (res.data.meta.status !== 200) {
					uni.$showMsg()
				}
				this.searchResults = res.data.message
			},
			// 
			toDetail(item) {
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.uni-searchbar {
		background-color: #c00000;
	}

	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.sugg-list {
		padding: 0 20rpx;

		.sugg-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 26rpx 0;
			border-bottom: 2rpx solid greenyellow;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				font-size: 30rpx;
			}
		}

	}
</style>
